package com.effvi.neo4j.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.EmbeddedGraphDatabase;


public class Demo1 {
	private static final String DB_PATH = "neo4j-store";  
    private static final String NAME = "name";  
    private static final String ID = "id";
  
    private static enum ExampleRelationshipTypes implements RelationshipType {  
        FANS , EXAMPLE,FOLLOW
    }  
  
    public static void main(final String[] args) throws IOException {  
        GraphDatabaseService graphDb = new EmbeddedGraphDatabase(DB_PATH);  
        registerShutdownHook(graphDb); 
//        Transaction tx = graphDb.beginTx();
//        Node n3 = graphDb.getNodeById(3L);
//        Iterator<Relationship> it =n3.getRelationships().iterator();
//        int i=0;
//        while(it.hasNext()){
//        	i++;
//        	Relationship r = it.next();
//        	r.delete();
//        	System.out.println(i);
//        }
//        n3.delete();
//        tx.success();
//        tx.finish();
//        System.out.println("sdfsdfsdfdsf");
//        Iterator<Node> it = graphDb.getAllNodes().iterator();
//        while(it.hasNext()){
//        	Node node = it.next();
//        	System.out.println(node.getId());
//        }
        String s = "src/main/resources/relationship_1338874769015.txt";
//        String s = "src/main/resources/tt.txt";
        File relationshipFile = new File(s);
        InputStream is = new FileInputStream(relationshipFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String data = null;
        int txMaxSize=20000;
        int i=1;
        int currentPage = 0;
        boolean hasData = true;
        Transaction tx = null;
        while(hasData){
        	if(tx == null){
        		tx = graphDb.beginTx();
        	}
        	data = reader.readLine();
        	if(data!=null){
        		System.out.println(i);
				if(!"".equals(data)){
					String[] split = data.split(",");
					long leftId = Long.valueOf(split[0]);
					long rightId = Long.valueOf(split[1]);
//        		System.out.println(data);
					Node leftNode = null;
					try {
						leftNode = graphDb.getNodeById(leftId);
					} catch (NotFoundException e) {
						e = null;
					}
					Node rightNode = null;
					try {
						rightNode = graphDb.getNodeById(rightId);
					} catch (NotFoundException e) {
						e = null;
					}
					if(leftNode == null){
						leftNode = graphDb.createNode();
						leftNode.setProperty(ID, leftId);
//        			leftNode.setProperty(NAME, split[2]);
					}
					if(rightNode == null){
						rightNode = graphDb.createNode();
						rightNode.setProperty(ID, rightId);
//        			rightNode.setProperty(NAME, split[3]);
					}
					rightNode.createRelationshipTo(leftNode, ExampleRelationshipTypes.FOLLOW);
					leftNode.createRelationshipTo(rightNode, ExampleRelationshipTypes.FANS);
				}
				tx.success();
				i++;
        	}else{
        		break;
        	}
        	
        }
       
        try {
			while((data = reader.readLine()) != null){
				if(i > txMaxSize){
					break;
				}
				System.out.println(i);
				
				if(!"".equals(data)){
					String[] split = data.split(",");
					long leftId = Long.valueOf(split[0]);
					long rightId = Long.valueOf(split[1]);
//        		System.out.println(data);
					Node leftNode = null;
					try {
						leftNode = graphDb.getNodeById(leftId);
					} catch (NotFoundException e) {
						e = null;
					}
					Node rightNode = null;
					try {
						rightNode = graphDb.getNodeById(rightId);
					} catch (NotFoundException e) {
						e = null;
					}
					if(leftNode == null){
						leftNode = graphDb.createNode();
						leftNode.setProperty(ID, leftId);
//        			leftNode.setProperty(NAME, split[2]);
					}
					if(rightNode == null){
						rightNode = graphDb.createNode();
						rightNode.setProperty(ID, rightId);
//        			rightNode.setProperty(NAME, split[3]);
					}
					rightNode.createRelationshipTo(leftNode, ExampleRelationshipTypes.FOLLOW);
					leftNode.createRelationshipTo(rightNode, ExampleRelationshipTypes.FANS);
				}
				tx.success();
				i++;
			}
		}finally{
			tx.finish();
		}
        System.out.println("sdfsdfs");
        // Encapsulate some operations in a transaction  
//        Transaction tx = graphDb.beginTx();  
//        try {  
//            // 下面创建两个节点 
//            Node firstNode = graphDb.createNode();  
//            Node secondNode = graphDb.createNode();  
//            Node thirdNode = graphDb.createNode();
//            firstNode.setProperty("id", 1L);
//            secondNode.setProperty("id", 2L);
//            thirdNode.setProperty("id", 3L);
//            firstNode.setProperty(NAME_KEY, "Hello");
//            secondNode.setProperty(NAME_KEY, "World");  
//            thirdNode.setProperty(NAME_KEY, " ha");  
//           
//            // 在两个节点间创建一个关系，也就是连接两个节点  
//            firstNode.createRelationshipTo(secondNode, ExampleRelationshipTypes.EXAMPLE);  
//            firstNode.createRelationshipTo(thirdNode, ExampleRelationshipTypes.FANS); 
//            secondNode.createRelationshipTo(thirdNode, ExampleRelationshipTypes.EXAMPLE);
////            String greeting = firstNode.getProperty(NAME_KEY) + " " + secondNode.getProperty(NAME_KEY);  
////            System.out.println(greeting);  
////            Relationship relationship = firstNode.getSingleRelationship(ExampleRelationshipTypes.EXAMPLE,  
////                    Direction.INCOMING); 
////            Relationship relationship1 = secondNode.getSingleRelationship(ExampleRelationshipTypes.EXAMPLE,  
////                    Direction.INCOMING);
////            System.out.println(relationship.getId());  
////            System.out.println(relationship.getEndNode().getProperty(NAME_KEY));  
////            System.out.println(relationship.getStartNode().getProperty(NAME_KEY));  
////            System.out.println(relationship1.getId());  
////            System.out.println(relationship1.getEndNode().getProperty(NAME_KEY));  
////            System.out.println(relationship1.getStartNode().getProperty(NAME_KEY));  
////            relationship.delete();  
////            relationship1.delete(); 
////            firstNode.delete();  
////            secondNode.delete(); 
////        	System.out.println(graphDb.getNodeById(4).getProperty(NAME_KEY));
//            tx.success();  
//        }  
//        finally {  
//            tx.finish();  
//        }  
        System.out.println("Shutting down database ..."); 
        graphDb.shutdown();  
    }  
  
    private static void registerShutdownHook(final GraphDatabaseService graphDb) {  
        // Registers a shutdown hook for the Neo4j instance so that it  
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the  
        // running example before it's completed)  
        Runtime.getRuntime().addShutdownHook(new Thread() {  
            @Override  
            public void run() {  
                graphDb.shutdown();  
            }  
        });  
    }  
}
